<?php if (!defined('BASEPATH'))exit('No direct script access allowed');
/**
 * 幸运抽奖活动
 */
class Lucky extends MY_Controller {

    function __construct() {
        parent::__construct();
        $this->load->model('account_model');
        $this->load->model('finance_list_model');
        $this->load->model('experience_list_model');
        $this->load->model('activity_lottery_list_model');
        $this->load->model('exchange_order_model');
        $this->load->model('exchange_list_model');
        $this->lucky_gifts = $this->config_award['locky_gifts'];
    }

    function index() {
        redirect("");
        $lucky_chances = $this->user_info['lucky_count'] ? $this->user_info['lucky_count'] : 0;
        $lottery_list = $this->activity_lottery_list_model->get_list(array(),0,60);
        foreach($lottery_list as $key=>$val){
            $lottery_list[$key]['username'] = name_formart($val['username']);
        }
        $this->assign("lucky_gifts", $this->lucky_gifts);
        $this->assign("lottery_list", $lottery_list);
        $this->assign("lucky_chances", $lucky_chances);
        $this->display('lucky/index.html');
    }
    //处理抽奖过程
    function lottery(){
        if($_POST){
            if(!$this->userinfo){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1001;
                $mess['errorMsg'] = "请登录后再进行操作";
                echo JSON($mess);exit;
            }
            //可抽奖次数
            $lucky_chances = $this->user_info['lucky_count'];
            if($lucky_chances == 0){
                $mess['reqStatus'] = false;
                $mess['errorCode'] = 1004;
                $mess['errorMsg'] = "很遗憾，没有抽奖机会了";
                echo JSON($mess);exit;
            }
            $account = $this->account_model->get($this->userinfo['user_id']);
            $str_arr = explode(",", "5,7,1,5,5,5,1,5,5,2,5,1,2,1,1,2,1,5,2,1,5,5,1,5,1,2,1,1,5,2,1,1,1,5,2,1,1,11,2,1,1,5,1,5,2,5,2,11,1,5,2,7,2,5,5,1,5,2,1,1,5,1,2,1,2,5,5,2,1,1,7,5,5,2,5,1,1,2,1,1,5,11,1,1,2,5,5,11,2,11,2,5,7,2,10,1,1,7,5,2");//指定奖品的集合
            $str_length = count($str_arr);//长度
            $prize_item_index = $str_arr[rand(0, $str_length-1)];//中奖的选项下标
            $prize_item = $this->lucky_gifts[$prize_item_index];//中奖奖品
            //更新用户信息
            $data = array();
            $data['lucky_count'] = $lucky_chances - 1;
            $this->users_model->user_update($data,$this->userinfo['user_id']);
            
            $type = $prize_item['type'];
            switch ($type){
                case "exper"://积分类型
                    $exper = $prize_item['value'];
                    //更新用户账户
                    $account_data = array();
                    $account_data['experience'] = $account['experience'] + $exper;//当前积分值
                    $account_data['experience_balance'] = $account['experience_balance'] +  $exper;//可用积分值
                    $this->account_model->update($account_data,$this->userinfo['user_id']);
                    //写入积分明细
                    $experience_list_data = array();
                    $experience_list_data['user_id'] = $this->userinfo['user_id'];
                    $experience_list_data['content'] = "参与【冷酷一夏】活动，获得".$exper."积分";
                    $experience_list_data['experience'] = $exper;
                    $experience_list_data['experience_balance'] = $account_data['experience_balance'];
                    $experience_list_data['reward_type'] = 'other_award';
                    $experience_list_data['type'] = 1;
                    $experience_list_data['create_time'] = time();
                    $experience_list_data['month'] = date("Ym");
                    $experience_list_data['day'] = date("Ymd");
                    $this->experience_list_model->insert($experience_list_data);
                    break;
                case "amount"://溜溜币类型
                    $amount = $prize_item['value'];
                    $account_data = array();
                    $account_data['total'] = $account['total']+ $amount;//账户总额
                    $account_data['balance'] = $account['balance'] + $amount;//账户余额
                    $this->account_model->update($account_data,$this->userinfo['user_id']);
                    
                    //写入溜溜币明细
                    $finance_list_data = array();
                    $finance_list_data['user_id'] = $this->userinfo['user_id'];
                    $finance_list_data['content'] = "参与【冷酷一夏】活动，获得".$amount."溜溜币";;
                    $finance_list_data['finance'] = $amount;
                    $finance_list_data['finance_balance'] = $account_data['balance'];
                    $finance_list_data['reward_type'] = 'other_award';
                    $finance_list_data['type'] = 1;
                    $finance_list_data['create_time'] = time();
                    $finance_list_data['month'] = date("Ym");
                    $finance_list_data['day'] = date("Ymd");
                    $this->finance_list_model->insert($finance_list_data);
                    break;
                case "recharge"://充值类型
                    
                    $exchange_order = array();
                    $exchange_order['account'] = $this->user_info['mobile'];
                    $exchange_order['user_id'] = $this->userinfo['user_id'];
                    $exchange_order['order_nid'] = MakeOrdId($this->userinfo['user_id']);//生成订单
                    $exchange_order['gift_id'] = 0;
                    $exchange_order['title'] = "【冷酷一夏】活动：".$prize_item['title'];
                    $exchange_order['amount'] = 0;
                    $exchange_order['type'] = 2;
                    $exchange_order['num'] = 1;
                    $exchange_order['status'] = 2;
                    $exchange_order['create_time'] = time();
                    $result = $this->exchange_order_model->insert($exchange_order);
                    if($result){
                        //兑换记录
                        $exchange_list_data = array();
                        $exchange_list_data['user_id'] = $exchange_order['user_id'];
                        $exchange_list_data['order_nid'] = $exchange_order['order_nid'];
                        $exchange_list_data['title'] = $exchange_order['title'];
                        $exchange_list_data['amount'] = $exchange_order['amount'];
                        $exchange_list_data['type'] = $exchange_order['type'];
                        $exchange_list_data['num'] = $exchange_order['num'];
                        $exchange_list_data['status'] = $exchange_order['status'];
                        $exchange_list_data['create_time'] = $exchange_order['create_time'];
                        $this->exchange_list_model->insert($exchange_list_data);
                    }
                    break;
                case "real"://实物类型
            }
            
            //添加抽奖记录
            $activity_data = array();
            $activity_data['user_id'] = $this->userinfo['user_id'];
            $activity_data['title'] = $prize_item['title'];
            $activity_data['type'] = $type;
            $activity_data['create_time'] = time();
            $this->activity_lottery_list_model->insert($activity_data);
            
            $mess['reqStatus'] = true;
            $mess['errorCode'] = 1000;
            $mess['prize_item'] = $prize_item_index;
            $mess['prize_title'] = $prize_item['title'];;
            $mess['prize_img'] = $prize_item['img'];
            echo JSON($mess);exit;
        }
    }
}
